package com.jdrx.ocp.api.app;

import com.jdrx.ocp.beans.dto.role.*;
import com.jdrx.ocp.service.RoleService;
import com.jdrx.platform.commons.rest.beans.dto.IdDTO;
import com.jdrx.platform.commons.rest.beans.enums.EApiStatus;
import com.jdrx.platform.commons.rest.beans.vo.ResposeVO;
import com.jdrx.platform.commons.rest.exception.BizException;
import com.jdrx.platform.commons.rest.factory.ResponseFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Optional;

import static com.jdrx.platform.common.support.gateway.GwConstants.TRANSPARENT_USERID_FEILD;

/**
 * @Author: Huangxg
 * @Date: 2018/11/20 14:25
 */
@RestController
@RequestMapping("/api/app/0/role/")
@Api(value = "app-角色管理", description = "app-角色管理")
public class RoleAppApi {

    @Autowired
    RoleService roleService;

    @RequestMapping(value = "queryRoleByPage", method = RequestMethod.POST)
    @ApiOperation(value = "角色查询（分页）", notes = "带数据分页的角色查询")
    public ResposeVO queryRoleByPage(@ApiParam(required = true) @RequestBody @Valid QueryRoleDTO queryRoleDTO) throws BizException {
        queryRoleDTO.setName(Optional.ofNullable(queryRoleDTO.getName()).orElse(null));
        return ResponseFactory.ok(roleService.queryRoleByPage(queryRoleDTO));
    }

    @RequestMapping(value = "queryRoles", method = RequestMethod.POST)
    @ApiOperation(value = "角色查询", notes = "角色查询")
    public ResposeVO queryRoles(@RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId,
                                @ApiParam(required = true) @RequestBody @Valid QueryRoleNoPageDTO queryRoleDTO) throws BizException {
        return ResponseFactory.ok(roleService.queryRoles(queryRoleDTO, uId));
    }

    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ApiOperation(value = "添加角色", notes = "添加角色")
    //@BizLogger(description = "添加角色", serviceId = Constants.SERVICE_ID)
    public ResposeVO create(@RequestBody @Valid CreateRoleDTO createRoleDTO,
                            @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId
                            //@RequestHeader(Constants.DEPT_PATH) String deptPath
    ) throws BizException {
        if (StringUtils.isBlank(createRoleDTO.getResourceAllIds()) && StringUtils.isBlank(createRoleDTO.getResourceIds())) {
            return ResponseFactory.err("请至少选择一组权限", EApiStatus.ERR_SYS);
        }
        //createRoleDTO.setDeptPath(deptPath);
        if (roleService.createRole(createRoleDTO, uId) > 0) {
            return ResponseFactory.ok("操作成功");
        } else {
            return ResponseFactory.err("添加角色失败", EApiStatus.ERR_SYS);
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "更新角色", notes = "更新角色")
    //@BizLogger(description = "更新角色", serviceId = Constants.SERVICE_ID)
    public ResposeVO update(@RequestBody @Valid UpdateRoleDTO updateRoleDTO,
                            @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId) throws BizException {
        if (roleService.updateRole(updateRoleDTO, uId) >= 0) {
            return ResponseFactory.ok("操作成功");
        } else {
            return ResponseFactory.err("更新角色失败", EApiStatus.ERR_SYS);
        }
    }

    @RequestMapping(value = "delete", method = RequestMethod.POST)
    @ApiOperation(value = "删除角色", notes = "删除角色")
    //@BizLogger(description = "删除角色", serviceId = Constants.SERVICE_ID)
    public ResposeVO delete(@RequestBody @Valid IdDTO<Long> idDTO,
                            @RequestHeader(TRANSPARENT_USERID_FEILD) Integer uId) throws BizException {
        return ResponseFactory.ok(roleService.deleteRole(idDTO, uId));
    }

    @RequestMapping(value = "awardedRoleResource", method = RequestMethod.POST)
    @ApiOperation(value = "分配角色资源", notes = "分配角色资源")
    //@BizLogger(description = "分配角色资源", serviceId = Constants.SERVICE_ID)
    public ResposeVO awardedRoleResource(@RequestBody @Valid RoleResourceDTO roleResourceDTO) throws BizException {
        if (roleService.awardedRoleResource(roleResourceDTO)) {
            return ResponseFactory.ok("操作成功");
        } else {
            return ResponseFactory.err("分配角色资源失败", EApiStatus.ERR_SYS);
        }
    }

    @RequestMapping(value = "queryRoleResource", method = RequestMethod.POST)
    @ApiOperation(value = "查询角色拥有的资源", notes = "根据角色查询角色对应的资源")
    public ResposeVO queryRoleResource(@RequestBody @Valid QueryRoleResourceDTO queryRoleResourceDTO) throws BizException {
        return ResponseFactory.ok(roleService.queryRoleResource(queryRoleResourceDTO.getRoleId()));
    }
}
